package xyz.doikki.dkplayer.widget.controller;

import android.content.Context;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import xyz.doikki.dkplayer.widget.component.DebugInfoView;
import xyz.doikki.dkplayer.widget.component.ProgressView;
import xyz.doikki.videocontroller.component.CompleteView;
import xyz.doikki.videocontroller.component.ErrorView;
import xyz.doikki.videocontroller.component.GestureView;
import xyz.doikki.videocontroller.component.LiveControlView;
import xyz.doikki.videocontroller.component.PrepareView;
import xyz.doikki.videocontroller.component.TitleView;
import xyz.doikki.videocontroller.component.VodControlView;
import xyz.doikki.videoplayer.controller.BaseVideoController;
import xyz.doikki.videoplayer.controller.IControlComponent;
import xyz.doikki.videoplayer.controller.IGestureComponent;
import xyz.doikki.videoplayer.player.VideoView;
import xyz.doikki.videoplayer.util.PlayerUtils;

/**
 * 抖音
 * Created by Doikki on 2018/1/6.
 */

public class TikTokController02 extends BaseVideoController implements
        GestureDetector.OnGestureListener,
        GestureDetector.OnDoubleTapListener,
        View.OnTouchListener{

    private GestureDetector mGestureDetector;

    public TikTokController02(@NonNull Context context) {
        super(context);
    }

    public TikTokController02(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TikTokController02(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {
        //显示调试信息
        addControlComponent(new DebugInfoView(getContext()));
        //addControlComponent(new ProgressView(getContext()));




    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {
        super.initView();
        mGestureDetector = new GestureDetector(getContext(), this);
        setOnTouchListener(this);
    }

    private int mCurPlayState;
    @Override
    public void setPlayState(int playState) {
        super.setPlayState(playState);
        mCurPlayState = playState;
    }

    private boolean isInPlaybackState() {
        return mControlWrapper != null
                && mCurPlayState != VideoView.STATE_ERROR
                && mCurPlayState != VideoView.STATE_IDLE
                && mCurPlayState != VideoView.STATE_PREPARING
                && mCurPlayState != VideoView.STATE_PREPARED
                && mCurPlayState != VideoView.STATE_START_ABORT
                && mCurPlayState != VideoView.STATE_PLAYBACK_COMPLETED;
    }

    @Override
    public boolean showNetWarning() {
        //不显示移动网络播放警告
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
//        if (isInPlaybackState()) {
//            mControlWrapper.toggleShowState();
//        }
//        return true;

        if (mIsDoubleTapTogglePlayEnabled && !isLocked() && isInPlaybackState()) togglePlay();
        return true;
    }
    private boolean mIsDoubleTapTogglePlayEnabled = true;

    @Override
    public boolean onDoubleTap(MotionEvent e) {
//        if (mIsDoubleTapTogglePlayEnabled && !isLocked() && isInPlaybackState()) togglePlay();
//        return true;

        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }
    private boolean mIsGestureEnabled = true;
    private int mSeekPosition = -1;
    private boolean mFirstTouch;
    private boolean mChangePosition;
    @Override
    public boolean onDown(MotionEvent e) {
        if (!isInPlaybackState() //不处于播放状态
                || !mIsGestureEnabled //关闭了手势
                || PlayerUtils.isEdge(getContext(), e)) //处于屏幕边沿
            return true;


        mFirstTouch = true;
        mChangePosition = false;

        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if (!isInPlaybackState() //不处于播放状态
                || !mIsGestureEnabled //关闭了手势
                || PlayerUtils.isEdge(getContext(), e1)) //处于屏幕边沿
            return true;
        float deltaX = e1.getX() - e2.getX();
        float deltaY = e1.getY() - e2.getY();
        if (mFirstTouch) {
            mChangePosition = Math.abs(distanceX) >= Math.abs(distanceY);
            if (!mChangePosition) {
                //半屏宽度
                int halfScreen = PlayerUtils.getScreenWidth(getContext(), true) / 2;
            }



            if (mChangePosition ) {
                for (Map.Entry<IControlComponent, Boolean> next : mControlComponents.entrySet()) {
                    IControlComponent component = next.getKey();
                    if (component instanceof IGestureComponent) {
                        ((IGestureComponent) component).onStartSlide();
                    }
                }
            }
            mFirstTouch = false;
        }
        if (mChangePosition) {
            slideToChangePosition(deltaX);
        }
        return true;
    }

    protected void slideToChangePosition(float deltaX) {
        deltaX = -deltaX;
        int width = getMeasuredWidth();
        int duration = (int) mControlWrapper.getDuration();
        int currentPosition = (int) mControlWrapper.getCurrentPosition();
        int position = (int) (deltaX / width * 120000 + currentPosition);
        if (position > duration) position = duration;
        if (position < 0) position = 0;
        for (Map.Entry<IControlComponent, Boolean> next : mControlComponents.entrySet()) {
            IControlComponent component = next.getKey();
            if (component instanceof IGestureComponent) {
                ((IGestureComponent) component).onPositionChange(position, currentPosition, duration);
            }
        }
        mSeekPosition = position;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //滑动结束时事件处理
        if (!mGestureDetector.onTouchEvent(event)) {
            int action = event.getAction();
            switch (action) {
//                case MotionEvent.ACTION_DOWN:
//                    if(isLongPress){
//                        Log.d("TAG_speed", "onLongPress: 1.0   ACTION_UP");
//                        mControlWrapper.setSpeed(2.0f);
//                    }
//                    break;
                case MotionEvent.ACTION_UP:
                    stopSlide();
                    if (mSeekPosition >= 0) {
                        mControlWrapper.seekTo(mSeekPosition);
                        mSeekPosition = -1;
                    }
                    if(isLongPress){
                        Log.d("TAG_speed", "onLongPress: 1.0   ACTION_UP");
                        mControlWrapper.setSpeed(1.0f);
                    }

                    break;
                case MotionEvent.ACTION_CANCEL:
                    stopSlide();
                    mSeekPosition = -1;
                    break;
            }
        }
        return super.onTouchEvent(event);
    }

    private void stopSlide() {
        for (Map.Entry<IControlComponent, Boolean> next : mControlComponents.entrySet()) {
            IControlComponent component = next.getKey();
            if (component instanceof IGestureComponent) {
                ((IGestureComponent) component).onStopSlide();
            }
        }
    }

    boolean isLongPress=false;

    @Override
    public void onLongPress(MotionEvent e) {
        int action = e.getAction();
        if(action==MotionEvent.ACTION_DOWN){
            isLongPress=true;
            mControlWrapper.setSpeed(2.0f);
            Log.d("TAG_speed", "onLongPress: 2.0   ACTION_DOWN");
        }else if(action==MotionEvent.ACTION_UP){
            //mControlWrapper.setSpeed(1.0f);
            //这里是ACTION_UP在onTouchEvent才能监听到
        }else if(action==MotionEvent.ACTION_CANCEL){
            //mControlWrapper.setSpeed(1.0f);
            Log.d("TAG_speed", "onLongPress: 1.0   ACTION_CANCEL");
        }else if(action==MotionEvent.ACTION_POINTER_UP){
            //mControlWrapper.setSpeed(1.0f);
            Log.d("TAG_speed", "onLongPress: 1.0   ACTION_POINTER_UP");
        }

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
}
